package com.example.app4.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.example.app4.R;
import com.example.app4.entil.User;

import java.util.List;

public class MyAdapter extends BaseAdapter {
    //使用list<Nate>,list会存储数据库中note表所有记录。
    private List<User> list;
    //用于将某个布局转换为view的对象。
    private LayoutInflater layoutInflater;


    int i = 0;

    private Handler handler = new Handler();
    private User user;

    ViewHolder viewHolder;

    private MyThread thread;

    //当创建MyAdapter对象的时候，需要list的数据
    public MyAdapter(List<User> list, Context context){
        this.list=list;
        layoutInflater=LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return list.size();
    }
 
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        if (convertView==null){
            convertView=layoutInflater.inflate(R.layout.item,null,false);
            viewHolder=new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        }else {
            viewHolder=(ViewHolder)convertView.getTag();
        }
        //将数据库中的内容加载到对应的控件上
        user = (User) getItem(position);


        viewHolder.tv_showName.setText(user.getName());
       // viewHolder.tv_showPhone.setText(user.getCount());



        thread = new MyThread();
        thread.start();


        return convertView;
    }
    //用于给item的视图加载数据内容。
    class ViewHolder implements View.OnClickListener {
        TextView tv_showName,tv_showPhone;

        Button tv_start, tv_stop;
        public ViewHolder(View view){
            tv_showName=view.findViewById(R.id.tv_showName);
            tv_showPhone=view.findViewById(R.id.tv_showPhone);
            tv_start=view.findViewById(R.id.tv_start);
            tv_stop=view.findViewById(R.id.tv_top);

            tv_start.setOnClickListener(this);
            tv_stop.setOnClickListener(this);

        }


        @Override
        public void onClick(View v) {


            if (v.getId() == R.id.tv_start) {

                thread.resumeThread(); // 恢复线程

            } else if (v.getId() == R.id.tv_top) {
                thread.stopThread(); // 暂停线程
            }
        }
    }

    class MyThread extends Thread {
        private boolean shouldContinue = true;
        private Object lock = new Object();
        private int finalI;
        private int stopfinalI;
        private int statrfinalI =0;


        @Override
        public void run() {
            while (shouldContinue) {
                // 执行上一步操作
                System.out.println("Begin to execute step 1...");
                try {

                    if (Integer.parseInt(user.getCount())==finalI){
                        stopfinalI = 0;

                        shouldContinue = false;
                    }

                    for (int i = statrfinalI; i <=Integer.parseInt(user.getCount()); i++) {

                        if (shouldContinue){

                            finalI = i;
                            handler.post(new Runnable() {
                                public void run() {

                                    //记录填充的进度
                                    //textView.setText("正在加载: " + progressStatus + "%");
                                    viewHolder.tv_showPhone.setText("正在加载: " + finalI + "");
                                }
                            });

                            try {

                                Thread.sleep(100); // 模拟耗时操作
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                System.out.println("End to execute step 1.");

                synchronized (lock) {
                    while (!shouldContinue) {
                        try {
                            lock.wait(); // 当 shouldContinue == false 时，线程等待
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }



            }
        }

        public void stopThread() {
            // 停止线程并唤醒
            synchronized (lock) {
                shouldContinue = false;
               lock.notify(); // 唤醒线程
                stopfinalI = finalI;

            }
        }

        public void resumeThread() {
            // 唤醒线程继续执行
            synchronized (lock) {
                shouldContinue = true;
                lock.notify(); // 唤醒线程

                statrfinalI = stopfinalI;
            }
        }

    }
}